package inet.app.vod.view;

import inet.app.vod.activity.R;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * used to display a grid view that can show a loadmore footerview.
 * 
 * @author huji
 * 
 */
public class FooterableGridView extends RelativeLayout {

	public static final int COUNT_PER_PAGE = 20;

	private GridView mGridView;

	private TextView loadMoreTextView;

	private OnClickListener mLoadMoreClickListener;

	public FooterableGridView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public FooterableGridView(Context context) {
		super(context);
		init();
	}

	private void init() {

		mGridView = new GridView(getContext());

		loadMoreTextView = (TextView) LayoutInflater.from(getContext()).inflate(
				R.layout.bold_text_view, null);
		loadMoreTextView.setOnClickListener(clickListener);
		LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.WRAP_CONTENT);
		lp.addRule(CENTER_HORIZONTAL);
		lp.addRule(ALIGN_PARENT_BOTTOM);
		loadMoreTextView.setLayoutParams(lp);

		mGridView.setOnScrollListener(footerViewListener);

		addView(mGridView);
		addView(loadMoreTextView);
	}

	public void setAdapter(ListAdapter adapter) {
		mGridView.setAdapter(adapter);
	}

	public void setOnItemClickListener(OnItemClickListener itemclick) {
		mGridView.setOnItemClickListener(itemclick);
	}

	public void setNumColumns(int num) {
		mGridView.setNumColumns(num);
	}

	public GridView getGridView() {
		return mGridView;
	}

	/**
	 * called this method after load data from the internet compkete. has more
	 * means if has more data to load.
	 * 
	 * @param hasmore
	 */
	public void loadComplete(boolean hasmore) {
		loadMoreTextView.setVisibility(View.VISIBLE);
		if (!hasmore) {
			mGridView.setOnScrollListener(null);
		}
	}

	public void setLoadMoreClickListener(OnClickListener listener) {
		mLoadMoreClickListener = listener;
	}
	
	public void setOnScrollListener(OnScrollListener listener) {
		mGridView.setOnScrollListener(listener);
	}

	private OnScrollListener footerViewListener = new OnScrollListener() {

		@Override
		public void onScrollStateChanged(AbsListView view, int scrollState) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onScroll(AbsListView view, int firstVisibleItem,
				int visibleItemCount, int totalItemCount) {
			if (firstVisibleItem == 0 && visibleItemCount == 0
					&& totalItemCount == 0) {
				return;
			}

			if (firstVisibleItem + visibleItemCount == totalItemCount) {
				if (loadMoreTextView.getVisibility() == View.GONE) {
					loadMoreTextView.setVisibility(View.VISIBLE);
				}
			} else {
				if (loadMoreTextView.getVisibility() == View.VISIBLE) {
					loadMoreTextView.setVisibility(View.GONE);
				}
			}
		}
	};

	private OnClickListener clickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			if (v.getVisibility() == INVISIBLE) {
				return;
			}

			loadMoreTextView.setVisibility(View.GONE);

			if (mLoadMoreClickListener != null) {
				mLoadMoreClickListener.onClick(v);
			}
		}
	};
}